package com.centling.smartSealForPhone.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.utils.ShowToast;

import java.io.File;

public class AlarmDetailActivity extends TitleBarActivity {
    private TextView tvSealName,tvOperaterName, tvStartTime, tvEndTime, tvMap, tvLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_detail);

        setTitleBarText("报警信息详情");

        initView();
        initData();
    }

    private void initView(){
        tvSealName= (TextView) findViewById(R.id.tv_alarm_detail_seal_name);
        tvOperaterName= (TextView) findViewById(R.id.tv_alarm_detail_operater_name);
        tvStartTime= (TextView) findViewById(R.id.tv_alarm_detail_start_time);
        tvEndTime= (TextView) findViewById(R.id.tv_alarm_detail_end_time);
        tvMap= (TextView) findViewById(R.id.tv_alarm_detail_map);
        tvLocation= (TextView) findViewById(R.id.tv_alarm_detail_location);
    }

    private void initData(){
        tvSealName.setText(getIntent().getStringExtra("sealName"));
        tvOperaterName.setText(getIntent().getStringExtra("operatorName"));
        tvStartTime.setText(getIntent().getStringExtra("startTime"));
        tvEndTime.setText(getIntent().getStringExtra("endTime"));
        tvLocation.setText(getIntent().getStringExtra("alarmLocation"));

        tvMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isInstallByRead("com.autonavi.minimap")) {
                    Intent gaoDeIntent = new Intent(Intent.ACTION_VIEW);
                    String url =
                            "androidamap://viewMap?sourceApplication=智能盖章&poiname=用印位置&lat="
                                    + getIntent().getStringExtra("latitude") + "&lon=" + getIntent().getStringExtra("longitude") + "&dev=0";
                    Uri uri = Uri.parse(url);
                    gaoDeIntent.setData(uri);
                    startActivity(gaoDeIntent);
                } else {
                    ShowToast.shortTime("您的手机没有安装高德地图");
                }
            }
        });
    }

    private boolean isInstallByRead(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }
}
